//
//  GXGroupSelectController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/3.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXGroupSelectController.h"
#import "GXGroupSelectFootView.h"
@interface GXGroupSelectController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView * selectTableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@property (nonatomic, strong) NSIndexPath *selectPath;//存放被点击的cell那行
@end

@implementation GXGroupSelectController
static NSString *const groupSelect=@"GroupSelect";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"分组";
    _selectPath = [NSIndexPath indexPathForRow:0 inSection:0];
    self.view.backgroundColor = kGlobalbgColor;
    [self requestData];
    [self creatAllView];
}
#pragma 请求分组数据
- (void)requestData
{
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [MenuContentManager selectMenuGroupWithStoreID:storeId success:^(id responseObject) {
        self.dataArray = [NSMutableArray arrayWithArray:responseObject];
        [self.selectTableView reloadData];
    } fail:^(id responseObject) {
    }];
}
- (void)creatAllView
{
    [self.view addSubview:self.selectTableView];
    [self setUpGroupSelectFootView];
}
#pragma mark -- 创建uitableview
- (UITableView *)selectTableView
{
    if (_selectTableView == nil) {
        _selectTableView =[[UITableView alloc] initWithFrame:CGRectMake(5,0,kUIScreenWidth-10,kUIScreenHeight-Height_NavBar-45) style:UITableViewStyleGrouped];
        _selectTableView.backgroundColor = kGlobalbgColor;
        _selectTableView.delegate=self;
        _selectTableView.dataSource=self;
        _selectTableView.sectionHeaderHeight = 20;
        _selectTableView.showsHorizontalScrollIndicator=NO;
        _selectTableView.showsVerticalScrollIndicator=NO;
        _selectTableView.editing =NO;
        _selectTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    }
    return _selectTableView;
}
- (void)setUpGroupSelectFootView
{
    UIButton * footBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    footBtn.height = 45;
    [footBtn setTitle:@"保存" forState:UIControlStateNormal];
    [footBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footBtn setBackgroundColor:RGBColor(90, 207, 193) forState:UIControlStateNormal];
    [footBtn setCornerWithRadius:3];
    [footBtn addTarget:self action:@selector(pressSaveBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.selectTableView.tableFooterView = footBtn;
}
- (void)pressSaveBtn:(UIButton *)btn
{
    if (_selectPath == [NSIndexPath indexPathForRow:0 inSection:0]) {
        self.selectModel =self.dataArray.firstObject;
    }
    WeakSelf(weakSelf)
    if (weakSelf.selectBlock) {
        weakSelf.selectBlock(weakSelf.selectModel);
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:groupSelect];
    if (!cell) {
    cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:groupSelect];
    GXMenuGroupModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 2;
    }
    if (_selectPath == indexPath) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//取消cell选中的效果
    //取消之前的选择
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selectPath];
    celled.accessoryType = UITableViewCellAccessoryNone;
    //记录当前的选择的位置
    _selectPath  = indexPath;
    //当前选择的打钩
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    GXMenuGroupModel * model = self.dataArray[indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.selectModel = model;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
@end
